package com.tomato.drugstore.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tomato.drugstore.VO.ResultVO;
import com.tomato.drugstore.entity.DrugCategory;
import com.tomato.drugstore.enums.ResultEnum;
import com.tomato.drugstore.form.SaveCategoryForm;
import com.tomato.drugstore.form.UpdateCategoryForm;
import com.tomato.drugstore.mapper.DrugCategoryMapper;
import com.tomato.drugstore.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "药品类目")
@RestController
@RequestMapping("/api/category")
@Slf4j
public class DrugCategoryControl {
    @Resource
    private DrugCategoryMapper drugCategoryMapper;

    @ApiOperation("获取所有类目")
    @GetMapping("/categories")
    public List<DrugCategory> categories() {
        log.info("获取类目列表");
        return drugCategoryMapper.selectList(null);
    }

    @ApiOperation("获取分页类目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = false,
                    dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "大小", required = false,
                    dataType = "Integer", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "categoryName", value = "类目名", required = false,
                    dataType = "string", paramType = "query", defaultValue = ""),
    })
    @GetMapping("/categoryPage")
    public ResultVO categoryPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "categoryName", defaultValue = "") String categoryName) {
        Page<DrugCategory> categoryPage = new Page<>(page, size);
        IPage<DrugCategory> categoryIPage = drugCategoryMapper
                .selectPage(categoryPage, new QueryWrapper<DrugCategory>()
                        .like("category_name", categoryName));
        return R.success(categoryIPage);
    }

    @ApiOperation("添加类目")
    @GetMapping("/save")
    public ResultVO save(@Valid SaveCategoryForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return R.error(ResultEnum.FORM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DrugCategory drugCategory = new DrugCategory();
        BeanUtils.copyProperties(form, drugCategory);
        try {
            drugCategoryMapper.insert(drugCategory);
        } catch (DuplicateKeyException e) {
            return R.error(ResultEnum.CATEGORY_EXIT_ERROR.getCode(), "类目已存在，添加失败");
        }
        return R.success("类目添加成功");
    }

    @ApiOperation("删除类目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类目id", required = true,
                    dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam(value = "id") Integer id) {
        int result = drugCategoryMapper.deleteById(id);
        if (result == 1) {
            return R.success("类目删除成功");
        } else {
            return R.error(ResultEnum.CATEGORY_NOT_EXIT_ERROR.getCode(), "类目不存在，删除失败");
        }
    }

    @ApiOperation("修改类目")
    @GetMapping("/update")
    public ResultVO update(@Valid UpdateCategoryForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return R.error(ResultEnum.FORM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DrugCategory drugCategory = new DrugCategory();
        BeanUtils.copyProperties(form, drugCategory);
        try {
            drugCategoryMapper.updateById(drugCategory);
        } catch (DuplicateKeyException e) {
            return R.error(ResultEnum.CATEGORY_EXIT_ERROR.getCode(), ResultEnum.CATEGORY_EXIT_ERROR.getMessage());
        }
        return R.success("修改类目成功");
    }
}
